package Api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"os"
	"path"
	"strconv"
	"time"
	"wonlicsys/utils/common"
)

//单文件上传
func FileUpload(c *gin.Context) {
	file, err := c.FormFile("File")
	saveDirParam := c.DefaultPostForm("saveDirParam ", "upload")
	fmt.Println(saveDirParam, "结果")
	if err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	if file.Size > 1024*1024*2 {
		common.JsonResult(c, 400, "文件太大了", "fail")
		return

	}
	//if file.Header.Get("Content-Type") != "image/png" {
	//	common.JsonResult(c, 400, "只允许上传png图片", "                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ")
	//	return
	//}
	//获取上传文件的后缀(类型)
	uploadFileNameWithSuffix := path.Base(file.Filename)
	uploadFileType := path.Ext(uploadFileNameWithSuffix)

	time_unix := strconv.FormatInt(time.Now().Unix()+int64(rand.Uint64()), 10)
	saveDir := "./" + saveDirParam
	filename := saveDir + "/" + time_unix + uploadFileType

	//打开目录
	localFileInfo, fileStatErr := os.Stat(saveDir)
	if fileStatErr != nil || !localFileInfo.IsDir() {
		//创建目录
		errByMkdirAllDir := os.MkdirAll(saveDir, 0755)
		if errByMkdirAllDir != nil {
			common.JsonResult(c, 400, "创建目录失败", "fail")
			return
		}
	}
	if err := c.SaveUploadedFile(file, filename); err != nil {
		common.JsonResult(c, 400, err.Error(), "fail")
		return
	}
	common.JsonResult(c, 200, "success", gin.H{
		"Res": true,
	})
}

//多文件上传
func FileUploads(c *gin.Context) {

}
